<?php
namespace app\common\util;

use think\exception\ValidateException;
use app\common\service\AdminPhoto;
use app\common\model\AdminPhoto as P_M;
class Upload
{

	//通用上传
	public static function putFile($file,$path){
		if(!$path) $path = 'default';
		try {
			validate(['file'=>[
				'fileSize' => 410241024,
				'fileExt' => 'jpg,jpeg,png,bmp,gif',
				'fileMime' => 'image/jpeg,image/png,image/gif', 
			]])->check(['file' => $file]);
		} catch (\think\exception\ValidateException $e) {
			return ['msg'=>'上传失败','code'=>201,'data'=>$e->getMessage()];
		}
		
		foreach($file as $k) {
			if(env('upload.file_type')==2){
				//阿里云上传
				$res = Oss::alYunOSS($k, $k->extension(),$path);
				if ($res["code"] == 201) return ['msg'=>'上传失败','code'=>201,'data'=>$res["msg"]];
				
				$name = $res['src'];
				AdminPhoto::add($k,$name,$path,2);
			} elseif (env('upload.file_type') == 3) {
				//七牛上传
				$res = Qiniu::QiniuOSS($k, $k->extension(), $path);
				if ($res["code"] == 201) {
					return ['msg' => '上传失败', 'code' => 201, 'data' => $res["msg"]];
				}
				
				$name = $res['src'];
				AdminPhoto::add($k, $name, $path, 3 , 1 , ["del_path"=>$res['key']]);
			}else{				
				// 生成文件类对象
				$temp_file=pathinfo($k);
				$temp_file=$temp_file['dirname'] . '/' . $temp_file['basename'];
				$fileUp = new \think\File($temp_file);
				
				// 如果保存目录不在，则创建
				$save_dir = 'upload/'.$path.'/'.date('Ymd').'/';
				$save_path = public_path().$save_dir;
				if(!is_dir($save_path)){
					@mkdir($save_path,0777,true);
				}
				// 保存文件名
				$filename =  $k->md5() . '.' . $k->getOriginalExtension();
				// 保存文件目录
				$savename = '/'.$save_dir.$filename;
				$name = str_replace("\\","/",$savename);
				//写入数据库
				AdminPhoto::add($k,$name,$path,1);
				// 添加数据口后再 临时文件保存本地
				$fileUp->move($save_path , $save_path.$filename);
			}
			$original_name=substr($k->getOriginalName(),0,50);
		}
		return ['msg'=>'上传成功','code'=>200,'data'=>['src'=>$name,'thumb'=>$name,'original_name'=>$original_name]];
	}

	// 抓取网络图片保存本地
	public static function getWebImages($url,$path){
		if(!$path) $path = 'webimg';
		$data=[];
		foreach($url as $i=>$k) {
			if($k!=""){
				if(env('upload.file_type')==2){
					return ['msg'=>'暂不支持远程抓图到阿里云','code'=>201];
				} elseif (env('upload.file_type') == 3) {
					return ['msg'=>'暂不支持远程抓图到七牛云','code'=>201];
				}else{
					$res=save_web_images($k);
					if($res['code']!=200){
						return ['code'=>$res['code'],'msg'=>$res['msg']];
						$data[$i]=[
							'url'=>$k,
							'status'=>0,
							'msg'=>$res['msg'],
						];
					}
					P_M::create([
					    'name' => $res['name'],
					    'href' => $res['href'],
					    'path' => $path,
					    'type' => 1,
						'get_type' => 2,
						'other' => '',
					    'ext' => $res['ext'],
					    'mime' => $res['mime'],
					    'size' => $res['size'],
					]);
					$data[$i]=[
						'url'=>$k,
						'src'=>$res['href'],
						'name'=>$res['name'],
						'size'=>$res['size'],
						'status'=>1,
						'msg'=>'上传成功！',
					];
				}
			}
		}
		return ['msg'=>'上传完成','code'=>200,'data'=>$data];
	}
   
}